package com.metrology.app.biz.impl.security;

import java.util.ArrayList;
import java.util.Collection;
import org.springframework.dao.DataAccessException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import com.metrology.app.biz.AdminBiz;
import com.metrology.app.domain.AdminUser;

public class MyUserDetailsService implements UserDetailsService {

	private AdminBiz adminBiz;
	public void setAdminBiz(AdminBiz adminBiz) {
		this.adminBiz = adminBiz;
	}
	@Override
	public UserDetails loadUserByUsername(String username)
			throws UsernameNotFoundException, DataAccessException {
		try {
			Collection<GrantedAuthority> auths = new ArrayList<GrantedAuthority>();
			// 得到用户的权限
			auths = adminBiz.loadUserAuthoritiesByUsername(username);
			// 取得用户信息
			AdminUser user = adminBiz.loadUserByUsername(username);
			user.setAuthorities(auths);
			return user;
		} catch (Exception e) {
			throw new UsernameNotFoundException(String.valueOf(username), e);
		}
	}

}
